package vinylInt 
{
	//loader
	import com.vinyl.net.ImageLoader;
	
	//confing
	import vinylInt.VinylIntISConfiguration;
	
	//deeplink
	import vinylInt.VinylIntSwfAddress;
	
	import com.vinyl.app.Application;
	import com.vinyl.app.Activity;
	import com.vinyl.app.Timeline;
	import com.vinyl.command.Call;
	import com.vinyl.controls.*;
	
	//data
	import com.vinyl.events.DataService;
	import com.vinyl.events.DataEvent;

	//sound & media share
	import com.vinyl.sound.SoundLoader;
	import com.vinyl.sound.SoundProxy;
	import com.vinyl.sound.SoundService;
	import com.vinyl.sound.SoundPlayer;
    import flash.media.Sound;
	
	//font & TextField share
	import com.vinyl.font.FontProxy;
	import com.vinyl.font.FontService;
	import com.vinyl.components.EmbedFontSimpleLable;
	import flash.text.Font;
	import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
    import flash.text.TextFormat;
	
	//internal
	import flash.system.ApplicationDomain;
	import flash.display.MovieClip;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.setTimeout;
	import flash.utils.clearInterval;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.display.StageScaleMode;
	
	/**
	 * ...
	 * @author 
	 */
	public class Main extends Application
	{
		//초기화 상태값
		private var _initFlag:Boolean;
		//프로세스 진행에 필요한 임시 변수
		private var _idd:Number;
		//임시 저장용 딥링크 주소
		private var _deepLink:String;
		//딥링크 오브젝트
		private var _swfAddress:VinylIntSwfAddress;
		//환경 설정
		private var _con:VinylIntISConfiguration;
		//공유 라이브러리 도메인
		private var _ad:ApplicationDomain;
		//에니메이션 엑티버트 오브젝트
		private var _activity:Activity;
		//엑티버트의 소유주 오브젝트
		private var _activityMc:MovieClip;
		//데이터 서비스 오브젝트
		private var _service:DataService;
		//컨텐트 로더
		private var _loader_sub_1:ImageLoader;
		private var _loader_sub_2:ImageLoader;
		
		/*
		 * constructor
		 */
		public function Main() 
		{
			//스테이지 설정
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			//초기화 상태 설정
			_initFlag = false;
			
			//딥링크 인스턴스 얻기
			_swfAddress = VinylIntSwfAddress.getInstance();
			
			//공유폰트 & 텍스트 필드
			var $fontService = FontService.getInstance();
			var $font_00 = $fontService.getFont( "shared/@sharedFont.swf" );
			$font_00.addEventListener( com.vinyl.events.DataEvent.GETDATA, function(e) { 
				trace( e.data.url ); 
			} );
			$font_00.load();
			
			//프로젝트 전체에 공유할 객체를 직접 가져오기
			//시스템 도메인과 별도로 영역을 둔다
			var $loader:Loader = new Loader(); 
			var $request:URLRequest = new URLRequest( "shared/vinyl int components.swf" ); 
			$loader.load( $request );
			$loader.contentLoaderInfo.addEventListener( Event.COMPLETE, $completeSharedListener );
			
			function $completeSharedListener(event:Event):void 
			{
				//리스너 재거
				event.target.removeEventListener( Event.COMPLETE, arguments.callee );
				_ad = event.target.applicationDomain;
				
				_loader_sub_1 = new ImageLoader();
				_loader_sub_1.onComplete = onComplete;
				_loader_sub_1.load( "sub_1.swf", new LoaderContext( false, _ad ) );
				
				_loader_sub_2 = new ImageLoader();
				_loader_sub_2.onComplete = onComplete;
				_loader_sub_2.load( "sub_2.swf",  new LoaderContext( false, _ad ) );
			}
			
			//딥링크 리스너 붙이기
			SWFAddress.addEventListener( SWFAddressEvent.CHANGE, handleSWFAddress );
			//SWFAddress.setValue( "/product/2/" );
		}
		
		/*
		 * ImageLoader의 핸들러
		 */
		private function onComplete( event, target )
		{
			//로더가 완료
			target.loader.content.x = 2000;
			target.loader.content.visible = false;
			
			(root as Object).container.addChild( target.loader.content );
			
			if ( _loader_sub_1.loader.content && _loader_sub_2.loader.content )
			{
				//모든 로딩이 끝남. 초기화 완료
				_initFlag = true;
			}
		}
		
		/*
		 * 딥링크를 위해서 버튼의 이벤트를 한곳에서 집중 처리한다.
		 */
		public function button_click( e )
		{
			_swfAddress.btnClick( e );
		}
		
		/*
		 * 라이브러리 도메인으로 부터 템틀릿 객체 얻기
		 */
		private function getShredObject( className:String ): Class
		{
			return _ad.getDefinition( className ) as Class;
		}
		
		/*
		 * SWFAddress handling
		 */
		private function handleSWFAddress(e:SWFAddressEvent):void
		{
			trace( e );
			////////////////////////////////////////////////////////////////////////
			//
			// 분기문이 너무 많아서 복잡함 고려대상
			//
			//	if ( VinylIntSwfAddress.handle ) VinylIntSwfAddress.handle( e );
				
				//주소창에서 직접 딥링크시
				if ( !_initFlag && e.pathNames.length > 1 )
				{
					_deepLink = e.value;
					SWFAddress.setValue( "/" + e.pathNames[0] + "/" );
					return;
				}
				
				//swfAddress
				if( !_initFlag ) 
				{				
					_idd = setTimeout( handleSWFAddress, 10, e );
					return;
				}
				else
				{
					if ( _idd != 0 ) clearInterval( _idd );	
					if( _deepLink ) SWFAddress.setValue( _deepLink );
					_deepLink = null;
				}
			//
			////////////////////////////////////////////////////////////////////////
			
			// 딥링크에 맞도록 컨텐츠를 구동한다.
			var $cate = e.value;
			
			//각각의 메뉴가 자신의 딥링크만을 처리한다.
			if ( e.pathNames.length > 1 )
			{
				if ( e.pathNames[0] == "sub_1" ) $cate = "/sub_1/";
				if ( e.pathNames[0] == "sub_2" ) $cate = "/sub_2/";
			}
			
			switch( $cate )
			{
				case "/sub_1/":
					_loader_sub_1.loader.content.visible = false;
					_loader_sub_2.loader.content.visible = false;
					
					_loader_sub_1.loader.content.x = 0;
					_loader_sub_1.loader.content.visible = true;
				break;	
				
				case "/sub_2/":
				
					_loader_sub_1.loader.content.visible = false;
					_loader_sub_2.loader.content.visible = false;
					
					_loader_sub_2.loader.content.x = 0;
					_loader_sub_2.loader.content.visible = true;
				break;
					
				case "/":
				break;
			}
			
			var $title:String = 'VINYL Website';	
			
			for (var i = 0; i < e.pathNames.length; i++) {
				$title += ' / ' + e.pathNames[i].substr(0,1).toUpperCase() + e.pathNames[i].substr(1);
			}
			
			SWFAddress.setTitle( $title );
			VinylIntSwfAddress.tmp = e.pathNames;
		}
	}
}